﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class BusinessUnitController : DefaultController
    {

        BusinessUnitService businessUnitService;

        public BusinessUnitController()
        {
            businessUnitService = new BusinessUnitService();
        }

        public ActionResult Index()
        {
            BusinessUnit businessUnit = businessUnitService.GetBusinessUnitByID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID);
            return View(businessUnit);
        }

        [HttpPost]
        public JsonResult Edit(BusinessUnit businessUnit)
        {
            BusinessUnit OriginalBU;
            JsonResult Response;

            OriginalBU = businessUnitService.GetBusinessUnitByID(businessUnit.BusinessUnitID);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (OriginalBU != null)
            {
                TryUpdateModel<BusinessUnit>(OriginalBU);

                businessUnitService.ModifyBusinessUnit(OriginalBU);

                if (businessUnit.UpdatePrice == true)
                    businessUnitService.UpdatePricesByBusinessUnitID(businessUnit.BusinessUnitID);

                Response.Data = true;
            }
            else
            {
                Response.Data = false;
            }

            return Json(Response);
        }

    }
}
